package com.ruoyi.project.system.controller;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TbBook;
import com.ruoyi.project.system.service.ITbBookService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 书籍Controller
 * 
 * @author ruoyi
 * @date 2020-03-06
 */
@RestController
@RequestMapping("/system/book")
public class TbBookController extends BaseController
{
    @Autowired
    private ITbBookService tbBookService;

    /**
     * 查询书籍列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBook tbBook)
    {
        startPage();
        List<TbBook> list = tbBookService.selectTbBookList(tbBook);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        if (!list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getStatus()!=null){
                    if (list.get(i).getStatus()==0){
                    jsonArray.getJSONObject(i).put("status1", "下架");
                    }else if (list.get(i).getStatus()==1){
                    jsonArray.getJSONObject(i).put("status1", "上架");
                    }
                }

            }

        }
        return getDataTable(jsonArray);
    }

    /**
     * 导出书籍列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:export')")
    @Log(title = "书籍", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbBook tbBook)
    {
        List<TbBook> list = tbBookService.selectTbBookList(tbBook);
        ExcelUtil<TbBook> util = new ExcelUtil<TbBook>(TbBook.class);
        return util.exportExcel(list, "book");
    }

    /**
     * 获取书籍详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbBookService.selectTbBookById(id));
    }

    /**
     * 新增书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:add')")
    @Log(title = "书籍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBook tbBook)
    {
        return toAjax(tbBookService.insertTbBook(tbBook));
    }

    /**
     * 修改书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:edit')")
    @Log(title = "书籍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBook tbBook)
    {
        return toAjax(tbBookService.updateTbBook(tbBook));
    }

    /**
     * 删除书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:remove')")
    @Log(title = "书籍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBookService.deleteTbBookByIds(ids));
    }
}
